<template lang="html">
  <div class="slider-container">
    <ul class="slider-list" ref="list">
      <li
        v-for="item, index in sliderList"
        class="slider-item"
        :class="item.color"
        :key="index"
        ref="item"
      >
        <div class="content"></div>
        <p class="title">{{item.title}}</p>
      </li>
    </ul>
  </div>
</template>

<script>
import Tool from '@/assets/script/tool';
export default {
  name: 'slider',
  props: ['sliderList'],
  methods: {
    init() {
      let list = this.$refs["list"];
      let items = this.$refs["item"];
      let width = 0,
          fontSize = parseFloat(Tool.css(document.documentElement, "fontSize"));
      if(items.length > 1) {
        width = parseFloat(Tool.css(items[0], "width")) + parseFloat(Tool.css(items[0], "marginRight"));
      } else {
        width = parseFloat(Tool.css(items, "width")) + parseFloat(Tool.css(items, "marginRight"));
      }
      list.style.width = items.length * width / fontSize + "rem";
    },
  },
  mounted() {
    this.init();
  }
}
</script>

<style lang="scss" scoped>
@import "../../assets/scss/default.scss";
// setBg
$colors: (red: $red, blue: $blue, green: $green, yellow: $yellow);
@mixin setBg {
  @each $key, $value in $colors {
    &.#{$key} {
      .content {
        @include bg($value);
      }
    }
  }
}
@mixin bg($color) {
  background-color: $color;
}
// text-overflow
@mixin textOverFlow {
  white-space:nowrap;
  overflow:hidden;
  text-overflow: ellipsis;
}
.slider-container {
  width: 100%;
  height: px2rem(255);
  overflow-x: scroll;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 0 px2rem(30);
  box-sizing: border-box;
}
.slider-list {
  &:after {
    content: '';
    display: block;
    clear: both;
  }
  .slider-item {
    width: px2rem(380);
    margin-right: px2rem(10);
    color: $white;
    float: left;
    &:last-child {
      margin-right: 0;
    }
    @include setBg;
    .content {
      width: 100%;
      height: px2rem(220);
    }
    .title {
      line-height: px2rem(35);
      font-size: px2rem(28);
      color: $default;
      width: 100%;
      @include textOverFlow;
    }
  }
}
</style>
